<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of lecturenotes
 *
 * @author mattia
 */
class lecturenotes {
    private $connection;
    private $datas;
    
    public function __construct() {
        $this->connection = Yii::app()->db;
        $this->datas = array();
        //echo "<script src='".__DIR__.DIRECTORY_SEPARATOR."js".DIRECTORY_SEPARATOR."app.js"."'></script>";
        echo "<script type=\"text/javascript\" src=\"http://code.jquery.com/jquery-1.11.1.min.js\"></script>";  
        echo "<script type='text/javascript'>";
        include_once __DIR__.DIRECTORY_SEPARATOR."js".DIRECTORY_SEPARATOR."app.js";
        echo "</script>";
    }
    
    private function createCategories(){
        
        $id = array();
        $name = array();
        $surname = array();
        
        $sql = "SELECT app_professors.id, app_matter.name, app_professors.name AS 'Name', app_professors.surname AS 'Surname' "
                . "FROM app_matter, app_professors, app_matter_professors "
                . "WHERE app_matter.id = app_matter_professors.idMatter AND app_professors.id = app_matter_professors.idProfessor"
                . " ORDER BY Surname";
        
        $command = $this->connection->createCommand($sql);
        $rows = $command->queryAll();
        
        foreach ($rows as $index => $value){
            $id[] = $value['id'];
            $name[] = $value['Name'];
            $surname[] = $value['Surname'];
        }
        $name = array_unique($name);
        $surname = array_unique($surname);
        
        foreach ($name as $index => $value){
            $this->datas[$index]['id']          = $id[$index];
            $this->datas[$index]['p_name']      = $value;
            $this->datas[$index]['p_surname']   = $surname[$index];
            
            foreach ($rows as $key => $row){
                if($id[$index] == $row['id']){
                    $this->datas[$index]['m_name'][] = $row['name'];
                }
            }
        }
    }

    public function getCategories(){
        $this->createCategories();
        
        echo "<ul id='lecturenotes' class='categories'>";
        foreach ($this->datas as $key => $value){
            echo "<li>";
                //echo "<a href=''>".$value['p_surname']." ".$value['p_name']."</a>";
                echo "<span class='professor'>";
                echo $value['p_surname']." ".$value['p_name'];
                echo "</span>";
                echo "<span class='name'>";
                echo "<a href=''>Visualizza</a>";
                echo "</span>";
                
                echo "<ul>";
                foreach ($this->datas[$key]['m_name'] as $m_key => $m_value){
                    echo "<li>";
                    echo "<a href=''>".$m_value."</a>";
                    echo "</li>";
                }
                echo "</ul>";
            echo "</li>";
        }
        echo "</ul>";
    }
}
